package atguigu.crowd.mvc.handler;

import atguigu.crowd.service.dao.RoleService;
import atguigu.crowd.util.ResultEntity;
import com.atguigu.crowd.config.Role;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    /**
     * 这只是ajax请求，复制返回数据的，数据如果前端没有接收的话，有默认接收样式，默认是直接打回到页面上的，别搞混了
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/role/get/page/info.json", produces = "application/json;charset=utf-8")
    @ResponseBody

    public ResultEntity<PageInfo<Role>> getAdminPage(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        // 查询得到分页数据
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);

        return ResultEntity.successWithData(pageInfo);
    }

    /**
     * 新增角色的数据
     *
     * @param role 前端ajax传的数据
     * @return
     */
    @RequestMapping(value = "/role/save.json", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> saveRole(Role role) {
        // 保存数据
        roleService.saveRole(role);

        return ResultEntity.successWithoutData();
    }

    /**
     * 更新角色的数据
     *
     * @param role 前端ajax传的数据
     * @return
     */
    @RequestMapping(value = "/role/update.json", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> updateRole(Role role) {
        // 保存数据
        roleService.updateRole(role);

        return ResultEntity.successWithoutData();
    }

    /**
     * 删除角色的数据
     *
     * @param role 前端ajax传的数据
     * @return
     */
    @RequestMapping(value = "/role/delete.json", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> deleteRole(Role role) {
        // 删除数据
        roleService.deleteRole(role);

        return ResultEntity.successWithoutData();
    }
}
